package com.woniu108.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.car.CarException;
import com.woniu108.carService.commos.exception.car.CarExceptionCode;
import com.woniu108.carService.commos.util.StaticData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.woniu108.car.client.PersonClient;
import com.woniu108.car.param.DeleteCarSeriesParam;
import com.woniu108.car.mapper.CarSeriesMapper;
import com.woniu108.car.mapper.CarStyleMapper;
import com.woniu108.car.model.CarSeries;
import com.woniu108.car.model.CarStyle;
import com.woniu108.car.param.BanCarSeriesParam;
import com.woniu108.car.param.InsertCarSeriesParam;
import com.woniu108.car.param.UnlockCarSeriesParam;
import com.woniu108.car.param.UpdateCarSeriesNameParam;
import com.woniu108.car.service.CarSeriesService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class CarSeriesServiceImpl extends ServiceImpl<CarSeriesMapper, CarSeries> implements CarSeriesService {
    @Resource
    private CarSeriesMapper carSeriesMapper;
    @Resource
    private PersonClient personClient;
    @Resource
    private CarStyleMapper carStyleMapper;


    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo==null){
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
        if (adminInfo.getAdminRoleId() != 1) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }
    @Override
    public List<CarSeries> findAllSeriesByBrandId(Integer brandId) {
        QueryWrapper<CarSeries> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId)
                .ne("series_state", StaticData.CAR_SERIES_STATE_DELETE);
        return carSeriesMapper.selectList(wrapper);
    }

    @Override
    public List<CarSeries> findAllNormalSeriesByBrandId(Integer brandId) {
        QueryWrapper<CarSeries> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId)
                .eq("series_state", StaticData.CAR_SERIES_STATE_NORMAL);
        return carSeriesMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertCarSeries(InsertCarSeriesParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        //查数据库中是否已存在该系列
        QueryWrapper<CarSeries> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", param.getBrandId())
                .eq("series_name", param.getSeriesName());
        CarSeries selectOne = carSeriesMapper.selectOne(wrapper);
        //判断是否已存在该系列
        if (selectOne != null) {
            //存在该系列，判断系列状态
            //正常或被禁用
            if (selectOne.getSeriesState().equals(StaticData.CAR_SERIES_STATE_BAN) || selectOne.getSeriesState().equals(StaticData.CAR_SERIES_STATE_NORMAL)) {
                throw new CarException(CarExceptionCode.CAR_SERIES_HAVE);
            }
            //若是被删除状态，则改为正常
            selectOne.setSeriesState(StaticData.CAR_SERIES_STATE_NORMAL);
            carSeriesMapper.updateById(selectOne);
        } else {
            //不存在该系列，新增系列
            CarSeries carSeries = new CarSeries();
            carSeries.setBrandId(param.getBrandId());
            carSeries.setSeriesName(param.getSeriesName());
            carSeries.setSeriesState(StaticData.CAR_SERIES_STATE_NORMAL);
            carSeriesMapper.insert(carSeries);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteCarSeries(DeleteCarSeriesParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        for (Integer seriesId : param.getSeriesIds()) {
            //删除汽车系列
            UpdateWrapper<CarSeries> seriesUpdateWrapper = new UpdateWrapper<>();
            seriesUpdateWrapper.set("series_state", StaticData.CAR_SERIES_STATE_DELETE);
            seriesUpdateWrapper.eq("series_id", seriesId);
            int seriesUpdate = carSeriesMapper.update(null, seriesUpdateWrapper);
            if (seriesUpdate <= 0) {
                throw new CarException(CarExceptionCode.CAR_SERIES_NOT_HAVE);
            }
            //删除款式
            UpdateWrapper<CarStyle> styleUpdateWrapper = new UpdateWrapper<>();
            styleUpdateWrapper.set("style_state", StaticData.CAR_STYLE_STATE_DELETE);
            styleUpdateWrapper.eq("series_id", seriesId);
            carStyleMapper.update(null, styleUpdateWrapper);
   }
        return true;
    }

    @Override
    public Boolean banCarSeries(BanCarSeriesParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        for (Integer seriesId : param.getSeriesIds()) {

            //禁用汽车系列
            CarSeries carSeries = new CarSeries();
            carSeries.setSeriesId(seriesId);
            carSeries.setSeriesState(StaticData.CAR_SERIES_STATE_BAN);
            CarSeries series = carSeriesMapper.selectById(seriesId);
            if (Objects.equals(series.getSeriesState(), StaticData.CAR_SERIES_STATE_DELETE)) {
                throw new CarException(CarExceptionCode.CAR_SERIES_STATE_DELETE);
            }
            if (Objects.equals(series.getSeriesState(), StaticData.CAR_SERIES_STATE_BAN)) {
                throw new CarException(CarExceptionCode.CAR_SERIES_STATE_BAN);
            }
            int update = carSeriesMapper.updateById(carSeries);
            if (update <= 0) {
                throw new CarException(CarExceptionCode.CAR_SERIES_NOT_HAVE);
            }

            //禁用款式
            UpdateWrapper<CarStyle> styleUpdateWrapper = new UpdateWrapper<>();
            styleUpdateWrapper.set("style_state", StaticData.CAR_STYLE_STATE_BAN);
            styleUpdateWrapper.eq("series_id", seriesId)
                    .ne("style_state", StaticData.CAR_STYLE_STATE_DELETE);
            carStyleMapper.update(null, styleUpdateWrapper);

               }
        return true;
    }

    @Override
    public Boolean unlockCarSeries(UnlockCarSeriesParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        for (Integer seriesId : param.getSeriesIds()) {
            //禁用汽车系列
            CarSeries carSeries = new CarSeries();
            carSeries.setSeriesId(seriesId);
            carSeries.setSeriesState(StaticData.CAR_SERIES_STATE_NORMAL);
            CarSeries series = carSeriesMapper.selectById(seriesId);
            if (Objects.equals(series.getSeriesState(), StaticData.CAR_SERIES_STATE_DELETE)) {
                throw new CarException(CarExceptionCode.CAR_SERIES_STATE_DELETE);
            }
            if (Objects.equals(series.getSeriesState(), StaticData.CAR_SERIES_STATE_NORMAL)) {
                throw new CarException(CarExceptionCode.CAR_SERIES_STATE_NORMAL);
            }
            int update = carSeriesMapper.updateById(carSeries);
            if (update <= 0) {
                throw new CarException(CarExceptionCode.CAR_SERIES_NOT_HAVE);
            }
            //解锁款式
            UpdateWrapper<CarStyle> styleUpdateWrapper = new UpdateWrapper<>();
            styleUpdateWrapper.set("style_state", StaticData.CAR_STYLE_STATE_NORMAL);
            styleUpdateWrapper.eq("series_id", seriesId)
                    .ne("style_state", StaticData.CAR_STYLE_STATE_DELETE);
            carStyleMapper.update(null, styleUpdateWrapper);


        }
        return true;
    }

    @Override
    public Boolean updateCarSeriesName(UpdateCarSeriesNameParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        CarSeries carSeries = carSeriesMapper.selectById(param.getSeriesId());
        if(carSeries==null){
            throw new CarException(CarExceptionCode.CAR_SERIES_NOT_HAVE);
        }
        if (Objects.equals(carSeries.getSeriesState(), StaticData.CAR_SERIES_STATE_DELETE)) {
            throw new CarException(CarExceptionCode.CAR_SERIES_STATE_DELETE);
        }
        carSeries.setSeriesName(param.getSeriesName());
        carSeriesMapper.updateById(carSeries);
        return true;
    }
}
